//
//  PopupMenuView.swift
//  BddShop
//
//  Created by xuchao on 2019/5/31.
//  Copyright © 2019 Fabric Keeper. All rights reserved.
//

import UIKit

@objc(BBPopupMenuItem)
class PopupMenuItem: NSObject {
    var title: String = ""
    var image: UIImage? = nil
    
    @objc
    init(title: String, image: UIImage?) {
        self.title = title
        self.image = image
    }
}

@objc(BBPopupMenuView)
class PopupMenuView: UIView {

    private weak var contentView: UIView!
    @objc
    private(set) weak var bgImageView: UIImageView!

    @objc
    var menuItems: [PopupMenuItem] = [] {
        didSet {
            contentView.subviews.forEach({ $0.removeFromSuperview() })
            var sum: CGFloat = 0.0
            for i in 0..<menuItems.count {
                let item = menuItems[i]
                let button = UIButton(type: .custom)
                button.translatesAutoresizingMaskIntoConstraints = false
                button.tag = i
                button.contentHorizontalAlignment = .left
                button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0)
                button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
                button.setImage(item.image, for: .normal)
                button.setTitle(item.title, for: .normal)
                button.setTitleColor(UIColor.white, for: .normal)
                contentView.addSubview(button)
                contentView.addConstraints([
                    NSLayoutConstraint(item: button, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .left, multiplier: 1.0, constant: 5.0),
                    NSLayoutConstraint(item: button, attribute: .right, relatedBy: .equal, toItem: contentView, attribute: .right, multiplier: 1.0, constant: -5.0),
                    NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1.0, constant: sum),
                    NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 48.0)
                ])
                sum += 48.0
                if i < menuItems.count - 1 {
                    let lineView = UIView()
                    lineView.backgroundColor = UIColor.clear
                    lineView.translatesAutoresizingMaskIntoConstraints = false
                    contentView.addSubview(lineView)
                    contentView.addConstraints([
                        NSLayoutConstraint(item: lineView, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .left, multiplier: 1.0, constant: 0.0),
                        NSLayoutConstraint(item: lineView, attribute: .right, relatedBy: .equal, toItem: contentView, attribute: .right, multiplier: 1.0, constant: 0.0),
                        NSLayoutConstraint(item: lineView, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1.0, constant: sum),
                        NSLayoutConstraint(item: lineView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 1.0)
                    ])
                    sum += 1.0;
                }
            }
            var rect = self.frame
            rect.size.height = CGFloat(48 * menuItems.count + 24)
            self.frame = rect
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        let bgImageView = UIImageView()
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(bgImageView)
        self.bgImageView = bgImageView
        self.addConstraints([
            NSLayoutConstraint(item: bgImageView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0),
            NSLayoutConstraint(item: bgImageView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0),
            NSLayoutConstraint(item: bgImageView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0.0),
            NSLayoutConstraint(item: bgImageView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0)
        ])

        let contentView = UIView()
        contentView.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(contentView)
        self.contentView = contentView
        self.addConstraints([
            NSLayoutConstraint(item: contentView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 16.0),
            NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 13.0),
            NSLayoutConstraint(item: contentView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: -16.0),
            NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -11.0)
        ])
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func setTarget(_ target: Any?, action: Selector) {
        contentView.subviews.filter({ $0 is UIButton }).forEach { (view) in
            (view as? UIButton)?.addTarget(target, action: action, for: .touchUpInside)
        }
    }
}
